package heima3_2.cn.osc.presenter.event_recent_presenter;

import android.content.Context;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import heima3_2.cn.osc.constant.Constants;
import heima3_2.cn.osc.model.bean.MoveDetailBean;
import heima3_2.cn.osc.presenter.BasePresenter;
import heima3_2.cn.osc.ui.main_fragment.event_particulars_fragment.EventRecentFragment;
import heima3_2.cn.osc.utils.PreferencesUtils;
import retrofit2.Call;
import retrofit2.Response;

/**
 * Created by Administrator on 2017/1/18 0018.
 */

public class RecentPresenter extends BasePresenter<MoveDetailBean> {
    private  Context mContext;
    private  EventRecentFragment mEventRecentFragment;
    private MoveDetailBean.PostBean mPost;

    public RecentPresenter(Context context, EventRecentFragment eventRecentFragment){
        this.mEventRecentFragment=eventRecentFragment;
        this.mContext=context;
    }
    @Override
    protected void getFailureData(Throwable t) {
            t.printStackTrace();
    }

    @Override
    protected void getSuccessData(Response<MoveDetailBean> response) {
        MoveDetailBean body = response.body();
        mPost = body.getPost();
        mEventRecentFragment.mWvParticulars.loadUrl(mPost.getUrl());
        mEventRecentFragment.mWvParticulars.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String request) {
                view.loadUrl(request);
                return super.shouldOverrideUrlLoading(view, request);
            }
        });

    }

    @Override
    public void getData(int page) {
        int newsID = PreferencesUtils.getInt(mContext, Constants.RECENT_ID);
        System.out.println("newsID"+newsID);
        if(newsID!=0){
            Call<MoveDetailBean> info = mRetrofitInterface.getRecent(newsID);
            info.enqueue(mCallback);

        }
    }
}
